package com.gardenguell.salad.data.client;

import com.gardenguell.salad.data.model.*;
import com.gardenguell.salad.data.util.EncryptUtil;
import com.gardenguell.salad.data.volley.OnErrorResponse;
import com.gardenguell.salad.data.volley.OnSuccessResponse;
import com.gardenguell.salad.data.volley.RequestManager;
import com.gardenguell.salad.data.volley.SaladRequest;
import com.google.gson.Gson;

/**
 * Created by absurd on 2015/12/5.
 */
public class SaladClient {

    public void login(String username, String password,
                      OnSuccessResponse<Token> onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.LOGIN)
                .add(Url.QueryKey.USERNAME, username)
                .add(Url.QueryKey.PASSWORD, EncryptUtil.md5(password))
                .toUrl();
        RequestManager.addRequest(new SaladRequest<Token>(url, Token.class, onSuccessResponse, onErrorResponse),
                "login");
    }


    public void register(String username, String password,
                         OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.REGISTER)
                .add(Url.QueryKey.USERNAME, username)
                .add(Url.QueryKey.PASSWORD, EncryptUtil.md5(password))
                .toUrl();
        RequestManager.addRequest(new SaladRequest<Token>(url, null, onSuccessResponse, onErrorResponse),
                "login");
    }


    public void getBoxList(String username,
                           OnSuccessResponse<Box> onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.GET_MAC_LIST)
                .bindAuthToken()
                .add(Url.QueryKey.USERNAME, username)
                .toUrl();
        RequestManager.addRequest(new SaladRequest<Box>(url, Box.class, onSuccessResponse, onErrorResponse),
                "get_box_list");
    }

    public void addBox(String mac, String username,
                        OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse) {
        String url = ParamsBuilder.create(Url.HOST + Url.ADD_MAC_TO_USER)
                .bindAuthToken()
                .add(Url.QueryKey.USERNAME, username)
                .add(Url.QueryKey.MAC, mac)
                .toUrl();
        RequestManager.addRequest(new SaladRequest(url, null, onSuccessResponse, onErrorResponse),
                "add_box");
    }


    public void removeBox(String mac, String username,
                       OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse) {
        String url = ParamsBuilder.create(Url.HOST + Url.REMOVE_MAC_FROM_USER)
                .bindAuthToken()
                .add(Url.QueryKey.USERNAME, username)
                .add(Url.QueryKey.MAC, mac)
                .toUrl();
        RequestManager.addRequest(new SaladRequest(url, null, onSuccessResponse, onErrorResponse),
                "remove_box");
    }

    public void getUnitList(String mac, String username,
                            OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.MAC_INFO)
                .bindAuthToken()
                .add(Url.QueryKey.MAC, mac)
                .add(Url.QueryKey.USERNAME, username)
                .toUrl();
        RequestManager.addRequest(new SaladRequest<Unit>(url, Unit.class, onSuccessResponse, onErrorResponse),
                "get_unit_list");
    }


    public void addVegetable(String mac, int unitId, String username, int cid,
                             OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.ADD_INSTANCE)
                .bindAuthToken()
                .add(Url.QueryKey.MAC, mac)
                .add(Url.QueryKey.UNIT, unitId+"")
                .add(Url.QueryKey.USERNAME, username)
                .add(Url.QueryKey.CID, cid + "")
                .toUrl();
        RequestManager.addRequest(new SaladRequest(url, null, onSuccessResponse, onErrorResponse),
                "add_vegetable");
    }

    public void finishVegetable(String mac, int unitId, String username,
                                OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.FINISH_INSTANCE)
                .bindAuthToken()
                .add(Url.QueryKey.MAC, mac)
                .add(Url.QueryKey.UNIT, unitId+"")
                .add(Url.QueryKey.USERNAME, username)
                .toUrl();
        RequestManager.addRequest(new SaladRequest(url, null, onSuccessResponse, onErrorResponse),
                "finish_vegetable");
    }


    public void getConfigSchema(String mac, int unitId, String username,
                                OnSuccessResponse<ConfigSchema> onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.GET_AUTO)
                .bindAuthToken()
                .add(Url.QueryKey.MAC, mac)
                .add(Url.QueryKey.UNIT, unitId + "")
                .add(Url.QueryKey.USERNAME, username)
                .toUrl();
        RequestManager.addRequest(new SaladRequest<ConfigSchema>(url, ConfigSchema.class, onSuccessResponse, onErrorResponse),
                "get_config_shcema");
    }

      public void setConfigSchema(String mac, int unitId, String username, int value,
                                  OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse){
          String url = ParamsBuilder.create(Url.HOST + Url.CHANGE_AUTO)
                  .bindAuthToken()
                  .add(Url.QueryKey.MAC, mac)
                  .add(Url.QueryKey.UNIT, unitId + "")
                  .add(Url.QueryKey.USERNAME, username)
                  .add(Url.QueryKey.VALUE, value + "")
                  .toUrl();
          RequestManager.addRequest(new SaladRequest(url, null, onSuccessResponse, onErrorResponse),
                  "set_config_schema");
      }

    public void setUnitEnvParams(String mac, int unit, String username, EnviromentParams envParams,
                                    OnSuccessResponse onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.SET)
                .bindAuthToken()
                .add(Url.QueryKey.MAC, mac)
                .add(Url.QueryKey.UNIT, unit+"")
                .add(Url.QueryKey.USERNAME, username)
                .toUrl();
        //PostEnviromentParam pep = new PostEnviromentParam();
        //pep.setUnitdata(envParams);
        Gson gson = new Gson();
        String requestBody = gson.toJson(envParams, EnviromentParams.class);
        RequestManager.addRequest(new SaladRequest(url, EnviromentParams.class, requestBody, onSuccessResponse, onErrorResponse),
                "set_enviroment_params");
    }




    public void getUnitEnvParams(String mac, int unitId, String username,
                          OnSuccessResponse<EnviromentParams> onSuccessResponse, OnErrorResponse onErrorResponse){
        String url = ParamsBuilder.create(Url.HOST + Url.QUERY)
                .bindAuthToken()
                .add(Url.QueryKey.MAC, mac)
                .add(Url.QueryKey.USERNAME, username)
                .add(Url.QueryKey.UNIT, unitId+"")
                .toUrl();
        RequestManager.addRequest(new SaladRequest<EnviromentParams>(url, EnviromentParams.class, onSuccessResponse, onErrorResponse),
                "get_unit_env_params");
    }

}
